package com.example.actionhandlers.actions;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import org.json.JSONObject;

import com.example.MyAudioManager;
import com.example.actionhandlers.AbstractAction;

public class MediaVolumeAction extends AbstractAction {

	public boolean isApplicable(JSONObject jsonObj) {
		return (jsonObj.has("STREAM_RING") || jsonObj.has("STREAM_MUSIC")
				|| jsonObj.has("STREAM_ALARM")
				|| jsonObj.has("STREAM_NOTIFICATION")
				|| jsonObj.has("STREAM_SYSTEM") || jsonObj
				.has("STREAM_VOICE_CALL")); // / Tell me when you use it !.
	}

	public void performAction(JSONObject jsonObj) throws Exception {
		if (MyAudioManager.getInstance() != null) {
			MyAudioManager.getInstance();
			HashMap<String, Integer> enums = MyAudioManager.getConstants();
			Iterator<Entry<String, Integer>> it = enums.entrySet().iterator();
			while (it.hasNext()) {
				Map.Entry<String, Integer> pairs = it.next();
				if (jsonObj.has(pairs.getKey())) {
					MyAudioManager.getInstance().setVolume(
							enums.get(pairs.getKey()),
							(Integer) jsonObj.get(pairs.getKey()));
				}
			}
		}
	}

	public String getActionName() {
		// TODO Auto-generated method stub
		return "volume";
	}

}
